var currentPage = 1;//当前页,即是页索引
var lastPage = 1;//最后一页
var pageSize=10;//每页多少条记录
var totaPage=0;
var totalRecord=0;
var noticeTime="",selectType="";
$(function(){
	$("#noticeSelect").click(noticeSelect);
	$("#gotoPageNum").click(gotoPageNum);
	$("#currentPage").keypress(pressEnter);		
});


function noticeSelect(){
	
	noticeTime=$("#date").val();
	selectType=$("#selectType").val();
	
	$.ajax( {
		url :"NoticeServlet",
		data : {
			action:"totalRecord",
			noticeTime :noticeTime,
			selectType :selectType
		},
		type :"post",
		async :false,
		success : function(data) {
			alert(data);
			var serverData = data;
			totalRecord = parseInt(serverData.toString());
			//totalPage=(totalRecord+pageSize)/pageSize+1;
			totalPage=parseInt((totalRecord%pageSize==0)?totalRecord/pageSize:totalRecord/pageSize+1);
			lastPage=totalPage;
			if (lastPage == 0) {
				currentPage = 0;
			}
		}
	});
	getSelectNoticeList();
	UpdatePaging();
	$("#page").css("display","block");	
}
function initTable(){
	var table=" <table border align='center'width='100%' id='table' cellspacing=0 cellpadding=0 border=1 style='border:  solid 1px #4B6DA3'>";						
		table+=" <thead style='background-color:#4B6DA3'>";						
        table+=" <tr><th width='5%'><input type='checkbox' id='allcheckbox' name='checkbox' onclick='checkbox()' value='all'/></th>";
        
        table+=" <th width='10%'>公告编号</th><th width='10%'>公告主题</th><th width='15%'>发布时间</th><th>公告内容</th>";
        
        table+=" </tr>";
        table+=" </thead>";
        table+=" </table>";
		$(table).appendTo($("#result"));		
}

//查询
function getSelectNoticeList(){
	$.ajax({
		url:"NoticeServlet",
		data:{
			action:"page",
			noticeTime :noticeTime,
			selectType :selectType,
			currentPage:currentPage,
			pageSize:pageSize
		},
		type:"post",
		dataType:"json",
		success:getSelectNoticeListSuccess
	});	
}

//成功后返回
function getSelectNoticeListSuccess(data){
	if(data==0){
		$("#result").html("没有数据，请重新查找");
		return;
	}else{
		$("#result").html("");
		initTable();
		for (i = 0; i < data.length; i++) {					
			var tbody = " <tbody>";
			tbody += " <tr><td><input type='checkbox' name='checkbox' value="+data[i].noticeID+" /></td>";

			tbody += " <td>";
			tbody += data[i].noticeID;
			tbody += " </td>";
		
			tbody += " <td>";
			tbody += data[i].noticeTitle;
			tbody += " </td>";
			var date=new Date(data[i].noticeTime.time);
			tbody += " <td>";
			tbody += date.toLocaleString();
			tbody += " </td>";
			
			tbody += " <td>";
			tbody += data[i].noticeContent;
			tbody += " </td>";

			tbody += " </tr>";
			tbody += " </tbody>";
			$(tbody).appendTo($("#table"));					
		}
	}
}


function delNotice(){
	var checkboxs=$(":checkbox");
	var noticeIDs="";
	for(var i=0;i<checkboxs.size();i++){
		if(checkboxs[i].checked){
			if(checkboxs[i].value!="all"){
				noticeIDs=noticeIDs+checkboxs[i].value+":";
			}
		}
	}
	alert(noticeIDs);
	$.ajax({
		url:"NoticeServlet",
		data:{
			action:"del",
			noticeIDs :noticeIDs
		},
		type:"post",
		dataType:"json",
		success:delNoticeSuccess
	});		
}

function delNoticeSuccess(data){
	if(1==data){
		alert("批量删除成功");
		currentPage = 1;
		getSelectNoticeList();
	}
}

function checkbox(){
	alert("sssss");
	var allcheckbox=$("#allcheckbox");
	alert(allcheckbox);
	var checkboxs=$(":checkbox");
	if(allcheckbox.checked){
		for(var i=0;i<checkboxs.size();i++){
			if(checkboxs[i].value!="all"){
				checkboxs[i].checked=true;
			}
		}
	}else{
		for(var i=0;i<checkboxs.length;i++){
			if(checkboxs[i].value!="all"){
				checkboxs[i].checked=false;
			}
		}
	}
}

function updateNotice(){
}

//首页
function FirstPage(evt){
	currentPage=1;
	$("#PageNum").html(currentPage);
	$("#nowPage").html(currentPage);
	evt.preventDefault();
	UpdatePaging();
	getSelectNoticeList();	
}
//最后一页
function LastPage(evt){
	currentPage=lastPage;
	$("#PageNum").html(currentPage);
	$("#nowPage").html(currentPage);
	evt.preventDefault();
	UpdatePaging();
	getSelectNoticeList();
}
//上一页
function PrevPage(evt) {
	currentPage--;
	$("#PageNum").html(currentPage);
	$("#nowPage").html(currentPage);
	evt.preventDefault();
	UpdatePaging();
	getSelectNoticeList();
}
//下一页
function NextPage(evt) {
	currentPage++;
	$("#PageNum").html(currentPage);
	$("#nowPage").html(currentPage);
	evt.preventDefault();
	UpdatePaging();
	getSelectNoticeList();
}
//键盘enter
function pressEnter(event) {
	if (event.keyCode == 13) {
		gotoPageNum();
	}
}
//快速跳到
function gotoPageNum(){
	if ($.trim($("#currentPage").val()) == "") {
		alert("页数不能为空！");
		return false;
	} else if ($("#currentPage").val() != parseInt($("#currentPage").val())
			|| $("#currentPage").val() > lastPage
			|| $("#currentPage").val() <= 0) {
		alert("页数应该为一个大于零小于总页数的整数！");
		return false;
	} else {
		currentPage = $.trim($("#currentPage").val());
		$("#PageNum").html(currentPage);
		$("#nowPage").html(currentPage);
		UpdatePaging();
		getSelectNoticeList();
	}
}


function UpdatePaging() {
	// If we're not on the first page, enable the "Previous" link.
	$("#totalPage").html(lastPage);
	$("#nowPage").html(currentPage);
	$("#PageNum").html(currentPage);
	if (currentPage != 1 && currentPage > 0) {
		$("#PrevPage").attr("href", "#");
		$("#PrevPage").click(PrevPage);
		$("#FirstPage").attr("href", "#");
		$("#FirstPage").click(FirstPage);
	} else {
		$("#PrevPage").removeAttr("href");
		$("#PrevPage").unbind("click");
		$("#FirstPage").removeAttr("href");
		$("#FirstPage").unbind("click");
	}

	// If we're not on the last page, enable the "Next" link.
	if (currentPage != lastPage && currentPage < lastPage) {
		$("#NextPage").attr("href", "#");
		$("#NextPage").click(NextPage);
		$("#LastPage").attr("href", "#");
		$("#LastPage").click(LastPage);
	} else {
		$("#NextPage").removeAttr("href");
		$("#NextPage").unbind("click");
		$("#LastPage").removeAttr("href");
		$("#LastPage").unbind("click");
	}
}
